/** tabdata4.do

This do-file takes disaggregated data from various sources including the census
and aggregates it into city proper, prefecture and various other levels of aggregation.

**/


clear
set mem 100m
set more off
capture log close
log using tabdata4.log, replace text


****** 1. Define generalized program that performs aggregation to be called multiple times *******

capture program drop aggregate
program define aggregate

/** This program takes one argument.  
It is an indicator for whether urban districts (unit_status==1)
should be counted negatively, or subtracted off of totals.  We set this argument
equal to 1 for cases in which this is a dataset of remainders between constant definition
city propers cp90 and actual city propers at t. **/

/*** Set all obs of variables to . if do not populate 
			all obs within (city05 year) groups. *****/
sort city05 year
by city05 year: gen num_districts = _N

foreach X of varlist a434-gdp_sect2_predf {
  egen varcount = count(`X'), by(city05 year)
  replace `X' = . if varcount<num_districts
  drop varcount
}

*** This says subtract off urban units, but add on rural units
if `1'==1 {
  foreach X of varlist a434-gdp_sect2_predf {
    replace `X' = -`X' if unit_status==0|unit_status==1
  }
}

cap gen promoted_ccity=.
cap gen unpromoted_ccity=.

sort city05 year city_name
by city05 year: replace city_name = city_name[1]
save tempcol.dta, replace

collapse (sum) a434-gdp_sect2_predf (max) promoted_ccity unpromoted_ccity cp90part cp10part, by(city05 year city_name)
sort city05 year
save tempcol2.dta, replace

**** Convert relevant 0s to missing values
use tempcol, clear
sort city05 year
by city05 year: keep if _n==1
foreach X of varlist a434-gdp_sect2_predf {
  rename `X' m_`X'
}
capture drop unpromoted_ccity promoted_ccity
merge city05 year using tempcol2.dta
tab _merge
drop _merge
foreach X of varlist a434-gdp_sect2_predf {
  replace `X' = . if m_`X'==.
  drop m_`X'
}

*** Label variables
label variable gdp "(ind yrbk)"
label variable nnp "(ind yrbk)"
label variable gdp_sector1 "(ind yrbk)"
label variable nnp_sector1 "(ind yrbk)"
label variable gdp_sector2 "(ind yrbk)"
label variable nnp_sector2 "(ind yrbk)"
label variable gdp_sector3 "(ind yrbk)"
label variable nnp_sector3 "(ind yrbk)"
label variable tot_pop "(ind yrbk)"
label variable r08_gdp "(ind yrbk)"
label variable r08_gdp_sector2 "(ind yrbk)"
label variable r08_nnp_sector2 "(ind yrbk)"
label variable r08_gdp_sector3 "(ind yrbk)"
label variable r08_nnp_sector3 "(ind yrbk)"
label variable r08_tot_pop "(ind yrbk)"
label variable arrived_fdi "(ind yrbk)"
label variable tot_emp "(ind yrbk)"
label variable tot_emp_sect2 "(ind yrbk)"
label variable tot_emp_sect3 "(ind yrbk)"
label variable num_car "(ind yrbk)"
label variable dust_ems "(ind yrbk)"
label variable so2 "(ind yrbk)"
label variable giov "(ind yrbk)"
label variable giov_soecol "(ind yrbk)"
label variable tot_area "(ind yrbk)"
label variable giov_new "(ind yrbk)"
label variable giov_soecol_new "(ind yrbk)"
label variable agr_mp "Total Agricultural Machinery Power (10000 Kw) (Michigan)"
label variable pop "Population at Year-end (10000 persons) (Michigan)"
label variable ruralpop "Rural Population at yr end (10000 persons) (Michigan)"
label variable rurallabor "Number of rural laborers (10000 persons) (Michigan)"
label variable gdp_michigan "gdp (100 millian yuan) (Michigan)"
label variable va_sec1 "Value Added of Primary Industry (100 million yuan) (Michigan)"
label variable va_sec2 "Value Added of Secondary Industry (100 million yuan) (Michigan)"
label variable expval "Value of Exports (1,000 US Dollars) (Michigan)"
label variable agr_rl "(fenxian)"
label variable culland "(fenxian to be merged to py)"
label variable fertilizer_1990 "(fenxian)"
label variable rurele "(fenxian)"
label variable gdp_fenxian "(fenxian)"
label variable gdp_sector1_fenxian "(fenxian)"
label variable gdp_sector2_fenxian "(fenxian)"
label variable gdp_f95 "(95 fiscal yearbook)"
label variable gdp_sector2_f95 "(95 fiscal yearbook)"
label variable pop_f95 "(95 fiscal yearbook)"
label variable rural_pop_f95 "(95 fiscal yearbook)"
label variable totemp " (py)"
label variable gdp_py " (py)"
label variable gdp_sect2 " (py)"
label variable gdp_sect3 " (py)"
label variable total_pop " (py)"
label variable kmpr " (py)"
label variable apr " (py)"
label variable prpc " (py)"
label variable num_bt " (py)"
label variable avgsalary " (py)"
label variable num_colstd " (py)"
label variable givo_soe " (py)"
label variable givo_colt " (py)"
label variable exp_lgt " (py)"
label variable num_hedu " (py)"
label variable publibcol " (py)"
label variable area " (py)"
label variable fixinvest " (py)"
label variable fdi_py "(py)"
label variable givo_py "(py)"
label variable c_totalPop "census total pop"
label variable c_emp "census total emp  "
label variable c_emp_ag "census agricultural emp  "
label variable c_emp_min "census mining emp  "
label variable c_emp_man "census manufacturing emp  "
label variable c_emp_man_trad "census manu trad emp "
label variable c_emp_man_chem "census manu chem emp "
label variable c_emp_man_hvy "census manu heavy emp "
label variable c_emp_man_hitech "census manu hightech emp "
label variable c_emp_util "census util emp  "
label variable c_emp_cons "census cons emp  "
label variable c_emp_soft "census soft emp  "
label variable c_emp_whsl "census wholesale emp  "
label variable c_emp_fina "census finance emp  "
label variable c_emp_res "census res emp  "
label variable c_emp_cul "census cul emp  "
label variable c_emp_pub "census pub emp  "
label variable c_emp_tran "census transportation emp  "
label variable c_rur_Mig "census rural migrant  "
label variable c_urb_Mig "census urban migrant  "
label variable c_rur_mig_19to55_ "census rural migrant 19-55 "
label variable c_rur_mig_19to55_h "census rural migrant 19-55 high"
label variable c_urb_mig_19to55_ "census urban migrant 19-55 "
label variable c_urb_mig_19to55_h "census urban migrant 19-55 high"
label variable c_f_19to55 "census female 19-55 "
label variable c_f_Under19 "census female <19  "
label variable c_f_Over55 "census female >55  "
label variable c_f_19to55_h "census female 19-55 high "
label variable c_f_19to55_w_ "census female 19-55 work "
label variable c_f_19to55_w_h "census female 19-55 work high"
label variable c_f_19to55_w_c "census female 19-55 work college "
label variable c_m_19to55 "census male 20394  "
label variable c_m_Under19 "census male <19  "
label variable c_m_Over55 "census male >55  "
label variable c_m_19to55_h "census male 19-55 high "
label variable c_m_19to55_w_ "census male 19-55 work "
label variable c_m_19to55_w_h "census male 19-55 work high"
label variable c_m_19to55_w_c "census male 19-55 work college"
label variable c_rur_mig_A "census rural migrantA  "
label variable c_urb_mig_A "census urban migrant A "
label variable c_rur_mig_19to55_A "census rural migrant 19-55 A"
label variable c_rur_mig_19to55_h_A "census rural migrant 19-55 high A"
label variable c_urb_mig_19to55_A "census urban migrant 19-55 A "
label variable c_urb_mig_19to55_h_A "census urban migrant 19-55 high A"
label variable c_rur_ob_ "census rural out birth  "
label variable c_urb_ob_ "census urban out birth  "
label variable c_rur_ob_19to55_ "census rural out birth 19-55 "
label variable c_rur_ob_19to55_h "census rural out birth 19-55 high"
label variable c_urb_ob_19to55_ "census urban out birth 19-55 "
label variable c_urb_ob_19to55_h "census urban out birth 19-55 high"
label variable c_rur_ob_A1 "census rural out birth A1 "
label variable c_urb_ob_A1 "census urban out birth A1 "
label variable c_rur_ob_19to55_A1 "census rural out birth 19-55 A1"
label variable c_rur_ob_19to55_h_A1 "census rural out birth 19-55 high A1"
label variable c_urb_ob_19to55_A1 "census urban out birth 19-55 A1 "
label variable c_urb_ob_19to55_h_A1 "census urban out birth 19-55 high A1"
label variable c_rur_ob_A2 "census rural out birth A2  "
label variable c_urb_ob_A2 "census urban out birth A2  "
label variable c_rur_ob_19to55_A2 "census rural out birth 19-55 A2 "
label variable c_rur_ob_19to55_h_A2 "census rural out birth 19-55 high A2"
label variable c_urb_ob_19to55_A2 "census urban out birth 19-55 A2 "
label variable c_urb_ob_19to55_h_A2 "census urban out birth 19-55 high A2"
label variable a101 "Total no. of H                                            Table 2-19"
label variable a102 "Total P                                                            "
label variable a103 "Total M                                   H=household(s)           "
label variable a104 "Total F                                   M=male(s)                "
label variable a105 "Residents of urban wards: No. of H        F=female(s)              "
label variable a106 "                     : P               P=population (M+F)       "
label variable a107 "                     : M                                        "
label variable a108 "                     : F                                        "
label variable a109 "Residents of villages: No. of H                                    "
label variable a110 "                  : P                                           "
label variable a111 "                  : M                                           "
label variable a112 "                  : F                                           "
label variable a113 "Residents of rural units outside village jurisdiction: No. of H    "
label variable a114 "                                                  : P           "
label variable a115 "                                                  : M           "
label variable a116 "                                                  : F           "
label variable a117 "Non-agricultural H: P                                              "
label variable a118 "               : M                                              "
label variable a119 "               : F                                              "
label variable a120 "Agricultural H: P                                                  "
label variable a121 "           : M                                                  "
label variable a122 "           : F                                                  "
label variable a123 "Specially designated H and persons outside any H: P                "
label variable a124 "                                             : M                "
label variable a125 "                                             : F                "
label variable a126 "All residents of all zhen (townships) combined: No. of H  Table 2-11"
label variable a127 "                                       : P                  "
label variable a128 "                                       : M                  "
label variable a129 "                                       : F                  "
label variable a130 "Town residents in all zhen combined: No. of H                      "
label variable a131 "                                 : P                             "
label variable a132 "                                 : M                             "
label variable a133 "                                 : F                             "
label variable a134 "Rural residents in all zhen combined: No. of H                     "
label variable a135 "                                  : P                            "
label variable a136 "                                  : M                            "
label variable a137 "                                  : F                            "
label variable a138 "Non-agricultural H in all zhen combined: P                Table 2-12"
label variable a139 "                                     : M                         "
label variable a140 "                                     : F                         "
label variable a141 "Agricultural H in all zhen combined: P                             "
label variable a142 "                              : M                             "
label variable a143 "                           : F                                "
label variable a144 "Specially designated H, etc. in all zhen combined: P               "
label variable a145 "                                            : M               "
label variable a146 "                                            : F               "
label variable a181 "Total P, all ages and both sexes                                    A1"
label variable a182 "Total M, all ages                                                   A2"
label variable a183 "Total F, all ages                                                   A3"
label variable a184 "P 0-4                                                               A4"
label variable a185 "M 0-4                                                               A5"
label variable a186 "F 0-4                                                               A6"
label variable a187 "P 5-9                                                               A7"
label variable a188 "M 5-9                                                               A8"
label variable a189 "F 5-9                                                               A9"
label variable a190 "P 10-14                                                            A10"
label variable a191 "M 10-14                                                            A11"
label variable a192 "F 10-14                                                            A12"
label variable a193 "P 15-19                                                            A13"
label variable a194 "M 15-19                                                            A14"
label variable a195 "F 15-19                                                            A15"
label variable a196 "P 20-24                                                            A16"
label variable a197 "M 20-24                                                            A17"
label variable a198 "F 20-24                                                            A18"
label variable a199 "P 25-29                                                            A19"
label variable a200 "M 25-29                                                            A20"
label variable a201 "F 25-29                                                            A21"
label variable a202 "P 30-34                                                            A22"
label variable a203 "M 30-34                                                            A23"
label variable a204 "F 30-34                                                            A24"
label variable a205 "P 35-39                                                            A25"
label variable a206 "M 35-39                                                            A26"
label variable a207 "F 35-39                                                            A27"
label variable a208 "P 40-44                                                            A28"
label variable a209 "M 40-44                                                            A29"
label variable a210 "F 40-44                                                            A30"
label variable a211 "P 45-49                                                            A31"
label variable a212 "M 45-49                                                            A32"
label variable a213 "F 45-49                                                            A33"
label variable a214 "P 50-54                                                            A34"
label variable a215 "M 50-54                                                            A35"
label variable a216 "F 50-54                                                            A36"
label variable a217 "P 55-59                                                            A37"
label variable a218 "M 55-59                                                            A38"
label variable a219 "F 55-59                                                            A39"
label variable a220 "P 60-64                                                            A40"
label variable a221 "M 60-64                                                            A41"
label variable a222 "F 60-64                                                            A42"
label variable a223 "P 65-69                                                            A43"
label variable a224 "M 65-69                                                            A44"
label variable a225 "F 65-69                                                            A45"
label variable a226 "P 70-74                                                            A46"
label variable a227 "M 70-74                                                            A47"
label variable a228 "F 70-74                                                            A48"
label variable a229 "P 75-79                                                            A49"
label variable a230 "M 75-79                                                            A50"
label variable a231 "F 75-79                                                            A51"
label variable a232 "P 80-84                                                            A52"
label variable a233 "M 80-84                                                            A53"
label variable a234 "F 80-84                                                            A54"
label variable a235 "P 85-89                                                            A55"
label variable a236 "M 85-89                                                            A56"
label variable a237 "F 85-89                                                            A57"
label variable a238 "P 90-94                                                            A58"
label variable a239 "M 90-94                                                            A59"
label variable a240 "F 90-94                                                            A60"
label variable a241 "P 95-99                                                            A61"
label variable a242 "M 95-99                                                            A62"
label variable a243 "F 95-99                                                            A63"
label variable a244 "P 100+                                                             A64"
label variable a245 "M 100+                                                             A65"
label variable a246 "F 100+                                                             A66"
label variable a261 "Total P aged 6+                                                     E1"
label variable a262 "Total M aged 6+                                                     E2"
label variable a263 "Total F aged 6+                                                     E3"
label variable a264 "P university                                                        E4"
label variable a265 "M                                                                   E5"
label variable a266 "F                                                                   E6"
label variable a267 "P technical/junior college                                          E7"
label variable a268 "M                                                                   E8"
label variable a269 "F                                                                   E9"
label variable a270 "P secondary technical school                                       E10"
label variable a271 "M                                                                  E11"
label variable a272 "F                                                                  E12"
label variable a273 "P senior middle school                                             E13"
label variable a274 "M                                                                  E14"
label variable a275 "F                                                                  E15"
label variable a276 "P junior middle school                                             E16"
label variable a277 "M                                                                  E17"
label variable a278 "F                                                                  E18"
label variable a279 "P primary school                                                   E19"
label variable a280 "M                                                                  E20"
label variable a281 "F                                                                  E21"
label variable a282 "P illiterate/semi-illiterate                                       E22"
label variable a283 "M                                                                  E23"
label variable a284 "F                                                                  E24"
label variable a291 "Total P 15+                                                         E1"
label variable a292 "Total M 15+                                                         E2"
label variable a293 "Total F 15+                                                         E3"
label variable a294 "P illiterate/semi-illiterate                                        E4"
label variable a295 "M                                                                   E5"
label variable a296 "F                                                                   E6"
label variable a301 "Total P 15+                                                         M1"
label variable a302 "Total M 15+                                                         M2"
label variable a303 "Total F 15+                                                         M3"
label variable a304 "P never married                                                     M4"
label variable a305 "M                                                                   M5"
label variable a306 "F                                                                   M6"
label variable a307 "P married                                                           M7"
label variable a308 "M                                                                   M8"
label variable a309 "F                                                                   M9"
label variable a310 "P widowed                                                          M10"
label variable a311 "M                                                                  M11"
label variable a312 "F                                                                  M12"
label variable a313 "P divorced                                                         M13"
label variable a314 "M                                                                  M14"
label variable a315 "F                                                                  M15"
label variable a321 "All births 1Jan89-30Jun90                                           B1"
label variable a322 "M births                                                            B2"
label variable a323 "F births                                                            B3"
label variable a324 "All births 1Jan89-30Jun89                                           B4"
label variable a325 "M births                                                            B5"
label variable a326 "F births                                                            B6"
label variable a327 "All births 1Jul89-31Dec89                                           B7"
label variable a328 "M births                                                            B8"
label variable a329 "F births                                                            B9"
label variable a330 "All births 1Jan90-30Jun90                                          B10"
label variable a331 "M births                                                           B11"
label variable a332 "F births                                                           B12"
label variable a341 "All deaths 1Jan89-30Jun90                                           D1"
label variable a342 "M deaths                                                            D2"
label variable a343 "F deaths                                                            D3"
label variable a344 "All deaths 1Jan89-30Jun89                                           D4"
label variable a345 "M deaths                                                            D5"
label variable a346 "F deaths                                                            D6"
label variable a347 "All deaths 1Jul89-31Dec89                                           D7"
label variable a348 "M deaths                                                            D8"
label variable a349 "F deaths                                                            D9"
label variable a350 "All deaths 1Jan90-30Jun90                                          D10"
label variable a351 "M deaths                                                           D11"
label variable a352 "F deaths                                                           D12"
label variable a361 "Total inmigrants                                                    R1"
label variable a362 "Within-province inmigrants: Total                                   R2"
label variable a363 "                       : from municipal cities                   R3"
label variable a364 "                       : from zhen (urban townships)             R4"
label variable a365 "                       : from xiang (rural townships)            R5"
label variable a366 "Inmigrants from other provinces: Total                              R6"
label variable a367 "                         : from municipal cities                 R7"
label variable a368 "                         : from zhen                             R8"
label variable a369 "                         : from xiang                            R9"
label variable a370 "Other inmigrants                                                   R10"
label variable a381 "Total employed P                                                    I1"
label variable a382 "Total          M                                                    I2"
label variable a383 "Total          F                                                    I3"
label variable a384 "P Agric./forestry/animal husb./fishery/water conservancy            I4"
label variable a385 "M                                                                   I5"
label variable a386 "F                                                                   I6"
label variable a387 "P Industry                                                          I7"
label variable a388 "M                                                                   I8"
label variable a389 "F                                                                   I9"
label variable a390 "P Mining, prospecting                                              I10"
label variable a391 "M                                                                  I11"
label variable a392 "F                                                                  I12"
label variable a393 "P Construction                                                     I13"
label variable a394 "M                                                                  I14"
label variable a395 "F                                                                  I15"
label variable a396 "P Transport, posts, telecommunications                             I16"
label variable a397 "M                                                                  I17"
label variable a398 "F                                                                  I18"
label variable a399 "P Commerce, supply and marketing                                   I19"
label variable a400 "M                                                                  I20"
label variable a401 "F                                                                  I21"
label variable a402 "P Real estate, utilities, residential services                     I22"
label variable a403 "M                                                                  I23"
label variable a404 "F                                                                  I24"
label variable a405 "P Medicine, health care, sports, welfare                           I25"
label variable a406 "M                                                                  I26"
label variable a407 "F                                                                  I27"
label variable a408 "P Education, culture, arts, radio, television                      I28"
label variable a409 "M                                                                  I29"
label variable a410 "F                                                                  I30"
label variable a411 "P Science, technology                                              I31"
label variable a412 "M                                                                  I32"
label variable a413 "F                                                                  I33"
label variable a414 "P Finance, insurance                                               I34"
label variable a415 "M                                                                  I35"
label variable a416 "F                                                                  I36"
label variable a417 "P Government, party, and NGOs                                      I37"
label variable a418 "M                                                                  I38"
label variable a419 "F                                                                  I39"
label variable a420 "P Other economic activities                                        I40"
label variable a421 "M                                                                  I41"
label variable a422 "F                                                                  I42"
label variable a431 "P Professional and high-level technical personnel                   O1"
label variable a432 "M                                                                   O2"
label variable a433 "F                                                                   O3"
label variable a434 "P Officials/managers in gov't, party, business, & NGOs              O4"
label variable a435 "M                                                                   O5"
label variable a436 "F                                                                   O6"
label variable a437 "P Clerical personnel                                                O7"
label variable a438 "M                                                                   O8"
label variable a439 "F                                                                   O9"
label variable a440 "P Employees in commercial sector                                   O10"
label variable a441 "M                                                                  O11"
label variable a442 "F                                                                  O12"
label variable a443 "P Employees in service sector                                      O13"
label variable a444 "M                                                                  O14"
label variable a445 "F                                                                  O15"
label variable a446 "P Workers in agric., forestry, animal husb., fisheries             O16"
label variable a447 "M                                                                  O17"
label variable a448 "F                                                                  O18"
label variable a449 "P Workers in manufacturing, construction, transport, etc.          O19"
label variable a450 "M                                                                  O20"
label variable a451 "F                                                                  O21"
label variable a452 "P Other and misc. occupations                                      O22"
label variable a453 "M                                                                  O23"
label variable a454 "F                                                                  024"
label variable a701 "P Han Chinese"
label variable a702 "M        "
label variable a703 "F        "
label variable a704 "P Mongol (Menggu) minority"
label variable a705 "M        "
label variable a706 "F        "
label variable a707 "P Hui minority"
label variable a708 "M        "
label variable a709 "F        "
label variable a710 "P Tibetan (Zang) minority"
label variable a711 "M        "
label variable a712 "F        "
label variable a713 "P Uygur (Weiwu`er) minority"
label variable a714 "M        "
label variable a715 "F        "
label variable a716 "P Miao minority"
label variable a717 "M        "
label variable a718 "F        "
label variable a719 "P Yi minority"
label variable a720 "M        "
label variable a721 "F        "
label variable a722 "P Zhuang minority"
label variable a723 "M        "
label variable a724 "F        "
label variable a725 "P Bouyei (Buyi) minority"
label variable a726 "M        "
label variable a727 "F        "
label variable a728 "P Korean (Chaoxian) minority"
label variable a729 "M        "
label variable a730 "F        "
label variable a731 "P Manchu (Man) minority"
label variable a732 "M        "
label variable a733 "F        "
label variable a734 "P Dong minority"
label variable a735 "M        "
label variable a736 "F        "
label variable a737 "P Yao minority"
label variable a738 "M        "
label variable a739 "F        "
label variable a740 "P Bai minority"
label variable a741 "M        "
label variable a742 "F        "
label variable a743 "P Tujia minority"
label variable a744 "M        "
label variable a745 "F        "
label variable a746 "P Hani minority"
label variable a747 "M        "
label variable a748 "F        "
label variable a749 "P Kazak (Hasake) minority"
label variable a750 "M        "
label variable a751 "F        "
label variable a752 "P Dai minority"
label variable a753 "M        "
label variable a754 "F        "
label variable a755 "P Li minority"
label variable a756 "M        "
label variable a757 "F        "
label variable a758 "P Lisu minority"
label variable a759 "M        "
label variable a760 "F        "
label variable a761 "P Va (Wa) minority"
label variable a762 "M        "
label variable a763 "F        "
label variable a764 "P She minority"
label variable a765 "M        "
label variable a766 "F        "
label variable a767 "P Gaoshan minority"
label variable a768 "M        "
label variable a769 "F        "
label variable a770 "P Lahu minority"
label variable a771 "M        "
label variable a772 "F        "
label variable a773 "P Shui minority"
label variable a774 "M        "
label variable a775 "F        "
label variable a776 "P Dongxiang minority"
label variable a777 "M        "
label variable a778 "F        "
label variable a779 "P Naxi minority"
label variable a780 "M        "
label variable a781 "F        "
label variable a782 "P Jingpo minority"
label variable a783 "M        "
label variable a784 "F        "
label variable a785 "P Kirgiz (Ke`erkezi) minority"
label variable a786 "M        "
label variable a787 "F        "
label variable a788 "P Tu minority"
label variable a789 "M        "
label variable a790 "F        "
label variable a791 "P Daur (Dawo`er) minority"
label variable a792 "M        "
label variable a793 "F        "
label variable a794 "P Mulam (Mulao) minority"
label variable a795 "M        "
label variable a796 "F        "
label variable a797 "P Qiang minority"
label variable a798 "M        "
label variable a799 "F        "
label variable a800 "P Blang (Bulang) minority"
label variable a801 "M        "
label variable a802 "F        "
label variable a803 "P Salar (Sala) minority"
label variable a804 "M        "
label variable a805 "F        "
label variable a806 "P Maonan minority"
label variable a807 "M        "
label variable a808 "F        "
label variable a809 "P Gelo (Gelao) minority"
label variable a810 "M        "
label variable a811 "F        "
label variable a812 "P Xibe (Xibo) minority"
label variable a813 "M        "
label variable a814 "F        "
label variable a815 "P Achang minority"
label variable a816 "M        "
label variable a817 "F        "
label variable a818 "P Pumi minority"
label variable a819 "M        "
label variable a820 "F        "
label variable a821 "P Tajik (Tajike) minority"
label variable a822 "M        "
label variable a823 "F        "
label variable a824 "P Nu minority"
label variable a825 "M        "
label variable a826 "F        "
label variable a827 "P Uzbek (Wuzibieke) minority"
label variable a828 "M        "
label variable a829 "F        "
label variable a830 "P Russian (Eluosi) minority"
label variable a831 "M        "
label variable a832 "F        "
label variable a833 "P Ewenki (Ewenke) minority"
label variable a834 "M        "
label variable a835 "F        "
label variable a836 "P De'ang minority"
label variable a837 "M        "
label variable a838 "F        "
label variable a839 "P Bonan (Bao`an) minority"
label variable a840 "M        "
label variable a841 "F        "
label variable a842 "P Yugur (Yugu) minority"
label variable a843 "M        "
label variable a844 "F        "
label variable a845 "P Jing minority"
label variable a846 "M        "
label variable a847 "F        "
label variable a848 "P Tatar (Tata`er) minority"
label variable a849 "M        "
label variable a850 "F        "
label variable a851 "P Drung (Dulong) minority"
label variable a852 "M        "
label variable a853 "F        "
label variable a854 "P Oroqen (Elunchun) minority"
label variable a855 "M        "
label variable a856 "F        "
label variable a857 "P Hezhen (Hezhe) minority"
label variable a858 "M        "
label variable a859 "F        "
label variable a860 "P Monba (Menba) minority"
label variable a861 "M        "
label variable a862 "F        "
label variable a863 "P Lhopa (Luoba) minority"
label variable a864 "M        "
label variable a865 "F        "
label variable a866 "P Jinuo minority"
label variable a867 "M        "
label variable a868 "F        "
label variable a869 "P nationality/ethnicity unknown"
label variable a870 "M                                     "
label variable a871 "F                                     "
label variable a872 "P naturalized citizens"
label variable a873 "M                       "
label variable a874 "F                       "
label variable promoted_ccity "Unit promoted to urban by 2008"
label variable unpromoted_ccity "Unit treated as urban but never promoted"
label variable gdp_predc "predicted gdp using census"
label variable gdp_predf "predicted gdp using fenxian"
label variable gdp_sect2_predc "predicted gdp using census"
label variable gdp_sect2_predf "predicted gdp using fenxian"
label variable asset_g_qz "Gross Assets (QZ)"
label variable asset_n_qz "Net Assets (QZ)"

*** Drop variables with 0 obs
foreach X of varlist a434-gdp_sect2_predf {
  qui: sum `X'
  if r(N)==0 {
	  drop `X'
  }
}

sort city05 year

end


********* 2. Call program to aggregate to various levels ******************

**** 1990 City Propers
use ..\..\data\tabular_data_BJ\generated\us123-census.dta, clear
keep if cp90==1
egen numpy = count(gdp_py), by(city05 year)
egen pop_py = sum(c_totalPop*(gdp_py~=.)), by(city05 year)
egen totpop = sum(c_totalPop), by(city05 year)
replace gdp_predf = gdp_py if gdp_py~=.
gen gdp_predxf = gdp_predf
replace gdp_predf = . if numpy==0
replace gdp_predf = . if pop_py/totpop<0.5
replace gdp_predc = gdp_py if gdp_py~=.
gen gdp_predxc = gdp_predc
replace gdp_predc = . if numpy==0 & year==1990
replace gdp_predc = . if pop_py/totpop<0.5 & year==1990
drop numpy pop_py
egen numpy = count(gdp_sect2), by(city05 year)
egen pop_py = sum(c_totalPop*(gdp_sect2~=.)), by(city05 year)
replace gdp_sect2_predf = gdp_sect2 if gdp_sect2~=.
replace gdp_sect2_predf = . if numpy==0
replace gdp_sect2_predf = . if pop_py/totpop<0.5
replace gdp_sect2_predc = gdp_sect2 if gdp_sect2~=.
replace gdp_sect2_predc = . if numpy==0 & year==1990
replace gdp_sect2_predc = . if pop_py/totpop<0.5 & year==1990
drop numpy pop_py totpop
aggregate 0
save ..\..\data\tabular_data_BJ\generated\agg_cp90.dta, replace

**** City Propers at time t (2005 defn Prefectures)
use ..\..\data\tabular_data_BJ\generated\us123-census.dta, clear
keep if unit_status==1|unit_status==0
*** Fill in imputed GDP, Sec 2 GDP for CP edges only, recorded in the pred variables
egen numpy = count(gdp_py), by(city05 year)
egen urban = sum(unit_status==1), by(city05 year)
replace gdp_predf = gdp_py if gdp_py~=. & urban==1 & numpy>0
replace gdp_predf = . if numpy==0
replace gdp_predc = gdp_py if gdp_py~=. & urban==1 & numpy>0
replace gdp_predc = . if numpy==0 & year==1990
drop numpy
egen numpy = count(gdp_sect2), by(city05 year)
replace gdp_sect2_predf = gdp_sect2 if gdp_sect2~=. & urban==1 & numpy>0
replace gdp_sect2_predf = . if numpy==0
replace gdp_sect2_predc = gdp_sect2 if gdp_sect2~=. & urban==1 & numpy>0
replace gdp_sect2_predc = . if numpy==0 & year==1990
drop urban numpy
aggregate 0
drop promoted_ccity cp90part cp10part
drop unpromoted_ccity
save ..\..\data\tabular_data_BJ\generated\agg_cpt.dta, replace

**** Prefecture Remainders at time t (2005 defn Prefectures)
use ..\..\data\tabular_data_BJ\generated\us123-census.dta, clear
drop if unit_status==1|unit_status==0
**** Fill out "predicted" GDP data with real data 
replace gdp_predf = gdp_py if gdp_py~=.
replace gdp_predc = gdp_py if gdp_py~=.
replace gdp_sect2_predf = gdp_sect2 if gdp_sect2~=.
replace gdp_sect2_predc = gdp_sect2 if gdp_sect2~=.
aggregate 0
drop promoted_ccity cp90part cp10part
drop unpromoted_ccity
save ..\..\data\tabular_data_BJ\generated\agg_noncpt.dta, replace

**** Full Prefectures at time t (2005 defn Prefectures)
use ..\..\data/tabular_data_BJ\generated\us123-census.dta, clear
**** Fill out "predicted" GDP data with real data 
replace gdp_predf = gdp_py if gdp_py~=.
replace gdp_predc = gdp_py if gdp_py~=.
replace gdp_sect2_predf = gdp_sect2 if gdp_sect2~=.
replace gdp_sect2_predc = gdp_sect2 if gdp_sect2~=.
aggregate 0
drop promoted_ccity cp90part cp10part
drop unpromoted_ccity
save ..\..\data\tabular_data_BJ\generated\agg_pref.dta, replace

**** 90 City Propers excluding areas in City Propers at time t
use ..\..\data\tabular_data_BJ\generated\us123-census.dta, clear
keep if cp90==1 | unit_status==1 | unit_status==0
drop if promoted_ccity==1
drop if unpromoted_ccity==1
drop if (unit_status==1 | unit_status==0) & cp90==1
tab year unit_status
replace gdp_predc = gdp_py if gdp_py~=.
replace gdp_predf = gdp_py if gdp_py~=.
replace gdp_sect2_predc = gdp_sect2 if gdp_sect2~=.
replace gdp_sect2_predf = gdp_sect2 if gdp_sect2~=.
aggregate 1
drop promoted_ccity cp90part cp10part
drop unpromoted_ccity
save ..\..\data\tabular_data_BJ\generated\agg_cp90remain.dta, replace


log close

erase tempcol.dta
erase tempcol2.dta

